package com.hrt.manage.product.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.hrt.base.BaseDaoImpl;
import com.hrt.bean.GiftDetail;
import com.hrt.bean.GiftInfor;
import com.hrt.manage.product.dao.GiftInforDao;

@Repository
public class GiftInforDaoImpl extends BaseDaoImpl<GiftInfor> implements GiftInforDao{

	@SuppressWarnings("unchecked")
	@Override
	public List<GiftInfor> findDelAll() {
		
		return getHibernateTemplate().find("FROM GiftInfor WHERE isDelete=0");
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<GiftInfor> queryGiftInfors(String itemCondition) {
		
			String hql="select gift from GiftInfor gift where gift.isdelete=0 and (gift.giftCode like '%"+itemCondition+"%'"+" or gift.giftName like '%"+itemCondition+"%'"+")";
			
			List<GiftInfor> results=new ArrayList<GiftInfor>();
			
			 results= getHibernateTemplate().find(hql);
			return results;
		
	}
	/**
	 * 根据礼品id查询礼品规格
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<GiftDetail> getGiftDetailByGiftId(Long giftId) {
		String hql = "select detail from GiftDetail detail where detail.isdelete=0 and detail.giftId='"+giftId+"' and detail.ispush=1";
		return getHibernateTemplate().find(hql);
	}
	
	/**
	 * 根据礼品ID 加载所有礼品规格（不分上下架）
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<GiftDetail> getGiftDetailsByGiftId(Long giftId) {
		String hql = "select detail from GiftDetail detail where detail.isdelete=0 and detail.giftId='"+giftId+"'";
		return getHibernateTemplate().find(hql);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<GiftInfor> queryByPageDate(Integer pageIndex, Integer pageSize,
			List<Map<String, Object>> conditions, String otherGiInput,
			String rankGiInput) {
		String hql = "select giInfo from GiftInfor giInfo";
		Map delcon = new HashMap();
		delcon.put("field", "isdelete");
		delcon.put("value", 0);
		conditions.add(delcon);
		for (int i = 0; i < conditions.size(); i++) {
			Map con = (Map) conditions.get(i);
			String field = con.get("field").toString();
			String val = con.get("value").toString();
			if (i == 0) {
				if(val != ""){
					hql = hql + " where giInfo." + field + " in(" + val+") and";
				}else{
					hql = hql +" where ";
				}
			} else {
				if("0".equals(rankGiInput)){
					System.out.println("aaaa");
					if("-1".equals(otherGiInput)){
						hql = hql + " giInfo." + field + "=" + val+" ORDER BY giInfo.createTime DESC";
					}else{
						hql = hql + " giInfo." + field + "=" + val+" and giInfo.ispush="+Integer.parseInt(otherGiInput)+" ORDER BY giInfo.createTime DESC";
					}
				}else if("1".equals(rankGiInput)){
					System.out.println("bbbb");
					if("-1".equals(otherGiInput)){
						hql = hql + " giInfo." + field + "=" + val+" ORDER BY giInfo.enterPrice DESC";
					}else{
						hql = hql + " giInfo." + field + "=" + val+" and giInfo.ispush="+Integer.parseInt(otherGiInput)+" ORDER BY giInfo.enterPrice DESC";
					}
				}else{
					if("-1".equals(otherGiInput)){
						System.out.println("ccc");
						hql = hql + " giInfo." + field + "=" + val+" ORDER BY giInfo.discount DESC";
					}else{
						hql = hql + " giInfo." + field + "=" + val+" and giInfo.ispush="+Integer.parseInt(otherGiInput)+" ORDER BY giInfo.discount DESC";
					}
				}
			}
		}
		System.out.println("hql==>"+hql);
		List<GiftInfor> results = super.queryByPage(hql, pageIndex, pageSize);
		return results;
	}

	

}
